Crazycatz00 / E[-X]Hentai Helper

// ==UserScript==
// @name        E[-X]Hentai Helper
// @namespace   Crazycatz00
// @description Auto transfer fjords; Inter-site links; Sad Panda login; etc.
// @icon        data:image/gif;base64,R0lGODlhLAAwAPQfAHFxcJKSki4uLtLQ0P7+/uHi4ru7u1hYWEVFRfX19dKjpAgICKSiomRWV1RHSIqIh2hoZ0dURzg4ODEiIj4+PjxFOxsaG1NTU0U8RGJiYn+Af11dXU1NTSQkJOzs7JOSkyH5BAUAAB8ALAAAAAAsADAAAAX/4CeOZGl+2QYA2+m+8AdcUMAMg2FoR+y/FI2h4PEEOBeAYQD4OUcNhieRIBQ6FqxAw2w+YZeOgZAoEgwCiUDQEXu8XxOgswgQitSzZS2xcBIFTHEmCxYSHndVBFZqEnQMd4EQgyNrCwCJBDlLG3sWdlREBnBxFxYLD3dWdB1pFhaQZGUFBAGUIp12iAwLCBoPG1gDsrMFBaSDkLsLFxoAFBYai1ShAwkMtyJSiAMdCBQC0YvEmkMeGtkB1osIC8xj43kJSwXX2R+piO0GiuS1scYm3XpwYx4tRWUUATAEQdGDbELGaaKViQEdAaYwaWpB6QGiehNlEbiwoEMGCCgt/4xJgOwJgAcPNMYjoGEBBwAZMhwIBoEMtlsLJowxs4iXLwgZLiBAIAABGUG3ODAw8IBWAQPQAjw4oPRbmlAPswlAJYoJBAYQLnBYqkYALQ8/gaYic0ZAAA0XukpQ02GYh1HZFlCckkHCAwgH1u6lsEBAlUAtnWjoMM3DAAoZWCSmIAFcnUUeCqA7IfDFhgygEwSQAACxUs5jpY0zQuoAmzWRRVAARZgCYq4I9r4aJhEtCQsQOFAI80KA3wQDEHDYcAG4o14SFxnIMAKAgwcOGjww6QLBlCkBBGw4sDM4m8/ZC5QWQIFpgwZ7T29AiRRjJA8QqLfTBeC0YQFFEh0jAv8AAnAgQXCdYQZBazlBsEBPxgywFnsHFDjWAdmB5sUzF9DHlgDsbaDiTgtIM0ABDFCQIlOt0GFHiOcsKEFOGNS3Rl5qcSBkHdAdQ4GKNLZhY4h3hOWgAgM00FQaS1W51AIMGIPVBRlwoAUFHHCFInGzjfaBBAwo4MCDi1Hg5lKMZXkOAoW1gsABOa1nygLwLCLaCBLc96CbnHXmCBawzNNhBxJMlwKHHLaC0AC2iLBXU0spZ6iBp4zRTlN5QspeXhzARwADYX3g4Jv1feOIAxw0daGFO+q0k62icoAcXQAsUMEIjLLRSiPfYYAABIxZcBKkK56WQl4IWLCBdq2AA0OpoBl00EAA4CCw3np45lShTkJKsABqCZD0DQbXwiaBA5x14KizKOVJnWKXnLoAZxPEcOQawbmJEX8nQQDuvcphsUQbAvQbAxIOOCDdYuLytyISirFBEHdPbCCBAmoW6u1OKurZFWzylvbEBQ4MoAB+nMm44qhsSTABFr/eYgHIxsaMhFoIYMBXKxHcIwKjGGD0poPhMBpBD0aXUK2bbdUXNQyxrkHB1SKEAAA7
// @match       *://*.e-hentai.org/*
// @match       *://exhentai.org/*
// @version     2.2.3
// @grant       none
// @noframes
// ==/UserScript==

if (window.location.hash === '#exhCookieLog')
{
	window.prompt("Make sure you are currently logged in!\nCode:", JSON.stringify(document.cookie.split(/;\s*/).filter(function(c){ return c.indexOf('ipb_') === 0; })));
}

var appendHeadLink = function(){
	var head, img;
	return function(txt, src){
		if (!head) { if (!(head = document.getElementById('nb'))) { return null; } }
		if (!img) { img = (head.getElementsByTagName('img')[0] || {}).src; }

		var e;
		if (img)
		{
			e = head.appendChild(document.createElement('img'));
			e.alt = '';
			e.src = img;
			head.appendChild(document.createTextNode(' '));
		}
		
		e = head.appendChild(document.createElement('a'));
		e.href = src;
		e.textContent = txt;
		return e;
	};
}(),

ehentai = function(){
	// --- Add Ex link
	var head = document.getElementById('nb3'), e;
	if (head)
	{
		e = head.parentNode;
		e.style.display = 'inline-block';
		e.style.width = head.style.width = 'auto';
		
		e = head.insertBefore(document.createElement('div'), head.children[head.children.length - 1]);
		e.style.cssFloat = 'left';
		
		e = e.appendChild(document.createElement('a'));
		e.href = '//exhentai.org/';
		
		e = e.appendChild(document.createElement('img'));
		e.alt = 'ExHentai Galleries';
		e.src = 'data:image/gif;base64,R0lGODdhcwAlAPUAAHRCD5tMCuHez1sODapYEaxqMKeihJlSBIBGBmc1BbuGUaxmKZtZFah9TXY7A1kyCalzOIQ9AahhHJ5lKpdcJG0pBJBFArJ1PZhMFbdrI4dVG7JoNZ5LAH1JGY9MAo9JDKNdNJxEAubk1amkht3ay9rXxqumiLWwk7Crjujg3bOukdXSweThzq6pjLmzmd/czdjVxM7Mt8S/qdPQv8zHs7y2nsjDrdDNur+7omA8Fq1yLp5eG7BuLlVAJIpPEMJ1QSwAAAAAcwAlAAAG/8CBcEgsGo/IpHLJbDqVhah0Sq1ar9isdsvtZg/gsHhMLpvP6LR6zUYH3vC4fE6v2+/4vH5/f/r/gIGCSiKFhoeIiYqLjI2Oj5CRjSmUlZaXmJmam5ydnp+gnCyjpKWmp6ipqqusra6vqwKys7S1tre4ubq7vL2+v8DBwsPExC/HyMnKy8zNzs/Q0dLTzyTW19jZ2tvc3d7f4OHi3iXl5ufo6err7O3u7/Dx7TD09fb3+Pn6+/z9/v8A+a0YSLCgwYMIEypcyLChw4cLZ0icSBFADoozcgDAKPGBxwQgE3To+CBkghwrZjzYyLFHj5Qcc5QM6WBkDpMgL6pEqXImSP8HLD+a1MCR4o2jSJMW8JDgaIwEHwokPZogAgMJBCjsmDAhhoMIFCDw2IEBg4UbDjBMpRpg7Y0HEQ4QuCoBAoQbHjhoxcDAAwIENwCcrRqAgGEJOnjE6IGAw47DIBq4RRqjsuXLMRogqBCjAoIGmCs72BFatATMPjjEQECgNADSoXP4iHGjNO3LcmNEUI2gQ23av3V/sE2chvHjyI1TiJDAwYTjETzmoJGAAA0EESw4+EvjA4PkCA5c/56cBgDyySt48GDhL/oHQAE4SADgAAQaFr5H8HAgQnYKx33AAQYIOPBVB+UlZ8OCDDa4oAYYgMAXg3RhYINgNnwQAAYehHD/gA0OEGBDdA/kEN6IHDi4oAMBqGgDAgGMRcBWDDoQwgILEJAXBQsi4MGIB0iww2MTNKiBBFcxgIAFGrjIoAxQRimlDKlJIIMEByAwJZQOHDClBgzIYAEBMlQYwAIyAPDBljI4sOaWDoTJJgATRAkCAVa26eUHGLDpJwd1+ikDDoQWaigFchVKwAE7GEroeh0UCAACH0iAQ1SOEpABDhSEYIFJHeCggV+g4vDGB9oBEAEAoVqwgaEMZIWDBQvg4IOHk34VAQI4zAfArxH4sEMDjjpaw7HIJqvAAsnWsIACzR4LwQUZ8LDBBRdAW0ADzW57LLU88FAABNzWMO0FC1x7/4G0OuiQgQ7jQuDsD81OoG25EFxbwAYLTFvDVToskEEB2UbbrAsIJ6zwwgw3vHANDkcsMcQSV2xxwxRHfMLGHHfs8ccghyzyyCSXbPLJI6ug8sost+zyyzDHLPPMNNdss8wo5Kzzzjz37PPPQAct9NBEFx10C0gnrfTSTDft9NNQRy311FRDbcLVWGet9dZcd+3112CHLfbYX49g9tlop6322my37fbbcMctt9sG1G333XjnrffefPft99+ABy744IQXbvjhhw+i+OKMN87H45BHLvnkd1hg+eWYZ6755px37vnnoIcuuucVlG766ainrvrqrLfu+uuwx95647TXbgR7EkEAADsK';
	}
},
gehentai = function(){
	var exURL = '//exhentai.org' + window.location.pathname;
	
	// --- Redirect fjords
	if (document.title.indexOf('Gallery Not Available') !== -1)
	{
		window.gotonext = function(){};
		window.stop();
		window.location.replace(exURL + '?fjords');
		return;
	}
	
	// --- Add Ex link
	appendHeadLink('ExHentai', exURL);
},
exhentai = function(){
	var e;
	
	// --- Sad Panda check
	if ((e = document.body.getElementsByTagName('img')).length === 1 && e[0].src === window.location.href)
	{
		exhentaiLogin();
		return;
	}
	
	// --- Add GE link if not fjords
	if (window.location.search.search(/[?&]fjords(?:[&=]|$)/i) === -1)
	{
		appendHeadLink('E-Hentai', '//g.e-hentai.org' + window.location.pathname);
	}
},
exhentaiLogin = function(){
	var container = document.createElement('div'),
		cookieText = container.appendChild(document.createElement('input')),
		submit = container.appendChild(document.createElement('input')),
		getCode = container.appendChild(document.createElement('a')),
		onEnter = function(e){ if (e.which === 13 && !submit.disabled) { onSubmit(); } },
		onSubmit = function(){
			if (submit.disabled) { return; }
			submit.disabled = true;
			try
			{
				var cookies = JSON.parse(cookieText.value);
				if (typeof cookies === 'object' && cookies.length > 0)
				{
					for (var i = 0; i < cookies.length; ++i)
					{
						document.cookie = cookies[i].replace(/(?:;\s*path=\S+|$)/, '; path=/').replace(/(?:;\s*domain=\S+|$)/, '; domain=.' + window.location.hostname).replace(/(?:;\s*max-age=\S+|$)/, '; max-age=31536000');
					}
					window.location.reload();
				}
			}
			catch (e)
			{
				alert(e);
				submit.disabled = false;
			}
		};
	
	container.style.textAlign = 'center';
	cookieText.type = 'text';
	cookieText.placeholder = 'Cookie Code';
	
	submit.type = 'button';
	submit.value = 'Login!';
	
	getCode.href= 'http://forums.e-hentai.org/index.php?act=Login#exhCookieLog';
	getCode.target = 'exhWindow';
	getCode.textContent = 'Get Code';
	getCode.style.cssText = '-moz-appearance: button; appearance: button; padding: 0 .2em; text-decoration: none; color: initial;';
	
	cookieText.addEventListener('keydown', onEnter, false);
	submit.addEventListener('click', onSubmit, false);
	
	document.body.appendChild(container);
},

general_viewer = function(){
	// --- Auto-run the failed image code after 3 seconds
	var loadFailSTO,
		loadFailCheck = function(){
			var img = document.getElementById('img');
			if (img.naturalWidth === 0)
			{
				if (typeof window.si !== 'number') { window.si = 1; }
				if (typeof window.nl === 'function') { window.nl(window.si); }
				else { window.location.search += (window.location.search[0] === '?' ? '&' : '?') + 'nl=' + window.si; }
			}
		},
		loadFail = function(){
			// Don't run it again if we already have (viewer sets this for us)
			if (window.location.search.search(/[?&]nl=/i) === -1)
			{
				loadFailSTO = setTimeout(loadFailCheck, 2500);
			}
		};
	
	loadFail();
	
	// Also affect dynamically loaded pages
	var proto = Object.getPrototypeOf(window.history);
	proto.origReplaceState = proto.replaceState;
	proto.replaceState = function(data, title, url){
		if (loadFailSTO) { clearTimeout(loadFailSTO); }
		window.history.origReplaceState(data, title, url);
		loadFail();
	};
};

switch (window.location.host)
{
	case 'e-hentai.org': ehentai(); break;
	case 'g.e-hentai.org': gehentai(); break;
	case 'exhentai.org': exhentai(); break;
}
if (window.location.pathname.indexOf('/s/') === 0) { general_viewer(); }